﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using NCI.EasyObjects;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using CMS.BLL.Entity;
using CMS.DAL;

namespace CMS.BLL
{
    public class Tours
    {
        #region
        public eTour Data;
        public Tours()
        {
            Data = new eTour();
        }
        ~Tours()
        { 
        }
        public virtual void Dispose()
        {
        }
        public int DeleteById(int ID)
        {
            int nResult = 0;
            cTour objDAL = new cTour();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.MarkAsDeleted();
                objDAL.Save();
                nResult = ID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }
        public void LoadById(int ID)
        {
            cTour objDAL = new cTour();
            objDAL.LoadByPrimaryKey(ID);
            ConvertData(objDAL);
        }
        public DataTable GetAll()
        {
            DataTable tbResult = new DataTable();
            cTour objDAL = new cTour();
            tbResult = objDAL.GetAll();
            return tbResult;
        }
        public int Insert()
        {
            int nResult = 0;
            cTour objInfor = new cTour();
            try
            {
                objInfor.AddNew();
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = objInfor.TourID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }

        public int Update(int ID)
        {
            int nResult = 0;
            cTour objInfor = new cTour();
            try
            {
                objInfor.LoadByPrimaryKey(ID);
                this.MappingData(objInfor);
                objInfor.Save();
                nResult = ID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }
        public int UpdatePriority(int TourID, int Priority)
        {
            int nResult = 0;
            cTour objDAL = new cTour();
            try
            {
                objDAL.LoadByPrimaryKey(TourID);
                objDAL.Priority = Priority;

                objDAL.Save();
                nResult = objDAL.TourID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }
        public int UpdateStatus(int ID, int Status)
        {
            int nResult = 0;
            cTour objDAL = new cTour();
            try
            {
                objDAL.LoadByPrimaryKey(ID);
                objDAL.Status = Status;
                objDAL.Save();
                nResult = objDAL.TourID;
            }
            catch
            {
                nResult = 0;
            }
            return nResult;
        }
        public DataTable Search(int ItemView, int ItemID, string Title, int StartDestinationID, int ArrivedDestinationID, int SaleOffID, int TourDEST, int DurationID, string TourCategoryID,
           int DefaultLangId, int CurrentLangId, int Status, int PageIndex, int PageSize, ref int PageCount)
        {
            DataTable tbResult = new DataTable();
            cTour objDAL = new cTour();
            tbResult = objDAL.Search(ItemView, ItemID, Title, StartDestinationID, ArrivedDestinationID, SaleOffID, TourDEST,DurationID, TourCategoryID, DefaultLangId,
                CurrentLangId, Status, PageIndex, PageSize, ref PageCount);
            return tbResult;
        }
        private void MappingData(cTour objInfor)
        {
            objInfor.StartDestinationID = Data.StartDestinationID;
            objInfor.SaleOffID = Data.SaleOffID;
            
            try{ objInfor.Title1 = Data.Title1; }
            catch{ objInfor.Title1 = ""; }
            try { objInfor.Title2 = Data.Title2; }
            catch { objInfor.Title2 = ""; }
            try { objInfor.Title3 = Data.Title3; }
            catch { objInfor.Title3 = ""; }

            try { objInfor.InitContent1 = Data.InitContent1; }
            catch { objInfor.InitContent1 = ""; }
            try { objInfor.InitContent2 = Data.InitContent2; }
            catch { objInfor.InitContent2 = ""; }
            try { objInfor.InitContent3 = Data.InitContent3; }
            catch { objInfor.InitContent3 = ""; }

            try { objInfor.Content1 = Data.Content1; }
            catch { objInfor.Content1 = ""; }
            try { objInfor.Content2 = Data.Content2; }
            catch { objInfor.Content2 = ""; }
            try { objInfor.Content3 = Data.Content3; }
            catch { objInfor.Content3 = ""; }

            try { objInfor.Price1 = Data.Price1; }
            catch { objInfor.Price1 = 0; }
            try { objInfor.Price2 = Data.Price2; }
            catch { objInfor.Price2 = 0; }
            try { objInfor.Price3 = Data.Price3; }
            catch { objInfor.Price3 = 0; }


            try { objInfor.Image = Data.Image; }
            catch { objInfor.Image = ""; }

            try { objInfor.StartDestinationID = Data.StartDestinationID; }
            catch { objInfor.StartDestinationID = 0; }

            try { objInfor.ArrivedDestinationID = Data.ArrivedDestinationID; }
            catch { objInfor.ArrivedDestinationID = 0; }

            try { objInfor.SaleOffID = Data.SaleOffID; }
            catch { objInfor.SaleOffID = 0; }

            objInfor.TourCategoryID = Data.TourCategoryID;
            objInfor.TourDurationID = Data.TourDurationID;
            objInfor.Priority = Data.Priority;
            objInfor.Status = Data.Status;
        }
        private void ConvertData(cTour objInfor)
        {
            Data.StartDestinationID = objInfor.StartDestinationID;
            Data.Title1 = objInfor.Title1;
            Data.Title2 = objInfor.Title2;
            Data.Title3 = objInfor.Title3;
            Data.InitContent1 = objInfor.InitContent1;
            Data.InitContent2 = objInfor.InitContent2;
            Data.InitContent3 = objInfor.InitContent3 ;
            Data.Content1= objInfor.Content1;
            Data.Content2=objInfor.Content2;
            Data.Content3 = objInfor.Content3;
            Data.Price1 = objInfor.Price1;
            Data.Price2 = objInfor.Price2;
            Data.Price3 = objInfor.Price3;
            Data.Image = objInfor.Image;
            Data.StartDestinationID = objInfor.StartDestinationID;
            Data.ArrivedDestinationID = objInfor.ArrivedDestinationID;
            Data.SaleOffID = objInfor.SaleOffID;
            Data.Priority = objInfor.Priority;
            Data.Status = objInfor.Status;

            Data.TourCategoryID = objInfor.TourCategoryID;
            Data.TourDurationID = objInfor.TourDurationID;
        }
        #endregion
    }
}
